package com.example.lib_home

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.webkit.JavascriptInterface
import android.webkit.WebSettings
import android.webkit.WebView
import androidx.activity.viewModels
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.example.lib_common.mvi.BaseActivity
import com.example.lib_home.databinding.ActivityHomeBinding

@Route(path = "/home/HomeActivity")
class HomeActivity : BaseActivity<ActivityHomeBinding,HomeViewModel>(){
    private lateinit var webSettings: WebSettings
    private lateinit var webview : WebView
    private lateinit var aidl: MainWebAIDL
    override fun createVM(): HomeViewModel {
        return viewModels<HomeViewModel>().value
    }

    override fun createViewBinding(): ViewBinding {
        return ActivityHomeBinding.inflate(inflater)
    }

    @SuppressLint("SetJavaScriptEnabled", "JavascriptInterface")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val intent = intent
        val url : String = intent.getStringExtra("url").toString()

        var intent1 = Intent()
        intent1.action = "com.kf.smartpark.main.MainWebService"
        intent1.`package` = "com.example.sys"
        bindService(intent1, connection, BIND_AUTO_CREATE)

        webview=v.web
        webview.loadUrl(url)
        webSettings=v.web.settings
        webSettings.javaScriptEnabled = true
        webSettings.allowFileAccess =true
        webSettings.setSupportZoom(true)
        webSettings.databaseEnabled=true
        webSettings.domStorageEnabled=true
        webview.addJavascriptInterface(object : WebViewJavaScriptFunction{
            override fun onJsFunctionCalled(tag: String) {
            }
            @JavascriptInterface
            fun androidparking(action :String){
                aidl.setWebName("androidparking")
            }
            @JavascriptInterface
            fun androidvisit(action :String){
                aidl.setWebName("androidvisit")
            }
            @JavascriptInterface
            fun androidapply(action :String){
                aidl.setWebName("androidapply")
            }
            @JavascriptInterface
            fun androidrepair(action :String){
                aidl.setWebName("androidrepair")
            }
            @JavascriptInterface
            fun androidaddrepair(action :String){
                aidl.setWebName("androidaddrepair")
            }
            @JavascriptInterface
            fun androidpatrol(action :String){
                aidl.setWebName("androidpatrol")
            }
        },"androidinfo")
    }
    override fun onDestroy() {
        super.onDestroy()
        if (webview != null)
            webview.destroy()
    }

    private val connection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            aidl = MainWebAIDL.Stub.asInterface(service)
        }

        override fun onServiceDisconnected(name: ComponentName?) {

        }

    }
}